/* 
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.appspot.dispotrains;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

public class GareDetailActivity extends FragmentActivity {
  private ShareActionProvider mShareActionProvider;
  private GareDetailFragment fragment;
  private Intent shareIntent;
  private int shareItemId;

  @Override
  @TargetApi(11)
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gare_detail);

    if (Build.VERSION.SDK_INT >= 11) {
      getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (savedInstanceState == null) {
      Bundle arguments = new Bundle();
      arguments.putInt(GareDetailFragment.ARG_ITEM_ID,
          getIntent().getIntExtra(GareDetailFragment.ARG_ITEM_ID, -1));
      fragment = new GareDetailFragment();
      fragment.setArguments(arguments);
      getSupportFragmentManager().beginTransaction().add(R.id.gare_detail_container, fragment)
          .commit();
    }
  }

  @TargetApi(14)
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem item = menu.add("Partager");
    this.shareItemId = item.getItemId();
    if (Build.VERSION.SDK_INT >= 11) {
      item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
    if (Build.VERSION.SDK_INT >= 14) {
      mShareActionProvider = new ShareActionProvider(this);
      item.setActionProvider(mShareActionProvider);
      mShareActionProvider.setShareIntent(getDefaultShareIntent());
    } else {
      mShareActionProvider = null;
    }

    return true;
  }

  private Intent getDefaultShareIntent() {
    Intent sendIntent = new Intent();
    return sendIntent;
  }

  // Call to update the share intent
  @TargetApi(14)
  void setShareIntent(Intent shareIntent) {
    this.shareIntent = shareIntent;
    if (mShareActionProvider != null) {
      mShareActionProvider.setShareIntent(shareIntent);
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      NavUtils.navigateUpTo(this, new Intent(this, GareListActivity.class));
      return true;
    } else if (item.getItemId() == this.shareItemId) {
      startActivity(Intent.createChooser(shareIntent, "Partager le status avec ..."));
    }

    return super.onOptionsItemSelected(item);
  }
}
